package vcp.client.GUI.mvc.controller;

import javax.swing.SwingUtilities;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.mvc.model.UIAbstractModel;
import vcp.client.GUI.mvc.model.UIUpdateCostsModel;
import vcp.client.GUI.mvc.view.UIAbstractView;
import vcp.client.GUI.mvc.view.UIUpdateCostsView;
import vcp.client.forms.SiteCostUpdateFrom;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.entity.Worker;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;

/**
 * 
 * @author VCP team
 *         <p>
 *         costs update controller
 *         </p>
 * @see UIUpdateCostsView
 * @see UIUpdateCostsModel
 * @see IFormSubmitionListener
 */
public class UIUpdateCostsController extends UIAbstractController implements IFormSubmitionListener <SiteCostUpdateFrom>
{

	/** reference to the view **/
	private final UIUpdateCostsView view;
	/** reference to the model **/
	private final UIUpdateCostsModel model;

	/**
	 * 
	 * @param view
	 *            to parent on screen
	 * 
	 * @param model
	 *            data of view
	 */
	public UIUpdateCostsController ( UIUpdateCostsView view, UIUpdateCostsModel model )
	{
		super(Screen.UPDATE_COSTS);
		this.view = view;
		this.model = model;
		this.setupListeners();
	}

	// ****************************************************************************************************************************
	// UIAbstractController methods
	// ****************************************************************************************************************************
	@Override
	public UIAbstractView getView ()
	{
		return this.view;
	}

	@Override
	public UIAbstractModel getModel ()
	{
		return this.model;
	}

	@Override
	public void clearMVC ()
	{
		this.view.setOnScreen(false);
	}

	@Override
	public void uploadDataContext ( Object data )
	{
		if ( data instanceof Worker )
		{
			this.flashData();
			Worker worker = (Worker) data;
			this.model.setSiteManager(worker);
			this.reloadGUI();

			this.model.shouldRegisterForUpdates();
			this.view.setOnScreen(true);
		}
	}

	@Override
	public void flashData ()
	{
		this.view.clearView();
		this.model.clearModel();
	}

	@Override
	protected synchronized void reloadGUI ()
	{
		this.view.loadSiteCosts(this.model.getWorkerSite());
		this.view.loadUpdateStatus(this.model.getWorkerSiteUpdate());
	}

	@Override
	protected void setupListeners ()
	{
		super.setupListeners();
		this.view.setFormSubmitionListener(this);
	}

	@Override
	public void onButtonPress ( Object object )
	{
		this.reloadGUI();
	}

	// ****************************************************************************************************************************
	// INotify methods
	// ****************************************************************************************************************************
	@Override
	public void updateControllerDelegateWithData ( Object newData )
	{
		this.parentDelegate.notifyOnDataUpdate(this.model.getSiteManager());
		if ( this.view.isOnScreen() )
		{
			if ( newData instanceof String )
			{
				String notification = (String) newData;
				this.parentDelegate.showPopUpMessage(notification, PopUpMessageType.INFORMATION);
			}
			else
			{
				SwingUtilities.invokeLater(new Runnable()
					{
						@Override
						public void run ()
						{
							UIUpdateCostsController.this.reloadGUI();
						}
					});
			}

		}
	}

	// ****************************************************************************************************************************
	// IFormSubmitionListener methods
	// ****************************************************************************************************************************
	@Override
	public void onFormSubmitted ( FormSubmitionEvent <SiteCostUpdateFrom> event )
	{
		this.model.applySiteCostUpdate(event.getForm());
	}
}
